<?php
declare(strict_types=1);
namespace cms\exam\api_controller;

use cms\exam\service\PaperService;
use cms\exam\service\RoomService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\RequestMapping;
use HyperfExt\Auth\Contracts\AuthManagerInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * Class PaperController
 * @package cms\exam\api_controller
 * @Controller(prefix="api/exam/paper")
 */
class PaperController extends CommonController
{
    /**
     * @Inject()
     * @var RoomService
     */
    protected RoomService $roomService;

    /**
     * @Inject()
     * @var PaperService
     */
    protected PaperService $paperService;

    /**
     * @return ResponseInterface
     * @RequestMapping(path="retrieve")
     */
    public function retrieve(): ResponseInterface
    {
        $app = $this->_getApp(true);
        $query = $this->paperService->model->where('app_id', $app->id);
        // region 查询条件部分 +++++
        $search = $this->request->input('search', '{}');
        $search = json_decode($search, true);
        if (isset($search['retest_enable'])) {
            $search['retest_enable'] = $search['retest_enable'] == 1 ? 1 : 2;
            $query->where('retest_enable', $search['retest_enable']);
        }
        // endregion

        $page_size = (int) $this->request->input('page_size', 10);
        $page_size = $page_size > 0 ? min($page_size, 50) : 1;
        $result = $query->paginate($page_size);
        return $this->success('请求成功', [
            'page' => $result->currentPage(),
            'total' => $result->total(),
            'items' => $result->items()
        ]);
    }

    /**
     * @return ResponseInterface
     * @RequestMapping(path="create")
     */
    public function create(): ResponseInterface
    {
        // 取得考场
        $room_id = (int) $this->request->input('room_id', 0);
        $room = $this->roomService->getDataById($room_id, true);
        $user = null;

        $query = $this->paperService->model->where('room_id', $room->id)->where('user_id', $user->id);
        // 取得试卷
        $paper = $query->first();
        if (!$paper) {
            $paper_data = [
                'app_id' => $room->app_id,
                'room_id' => $room->id,
                'user_id' => $user->id,
                'name' => $room->name.'--'.$user->name,
                'content' => $this->roomService->createPaperContent($room)
            ];
            if (!$paper_data['content']) {
                return $this->error('试卷生成失败');
            }
            return $this->success('请求成功', $paper_data);
            // $paper = $this->paperService->model->create($paper_data);
        }
        return $this->success('请求成功', $paper);
    }

    /**
     * @return ResponseInterface
     * @RequestMapping(path="detail")
     */
    public function detail(): ResponseInterface
    {
        $id = (int) $this->request->input('id', 0);
        $data = $this->paperService->getDataById($id, true);
        return $this->success('请求成功', $data);
    }
}